<!DOCTYPE html>
<html lang="zh-CN">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Promise AJAX</title>
</head>

<body>
    <script>
        function ajax(URL) {
            return new Promise(function (resolve, reject) {
                var req = new XMLHttpRequest();
                req.open('GET', URL, true);
                req.onload = function () {
                    if (req.status === 200) {
                        resolve(req.responseText);
                    } else {
                        reject(new Error(req.statusText));
                    }
                };
                req.onerror = function () {
                    reject(new Error(req.statusText));
                };
                req.send();
            });
        }
        var URL = "/try/ajax/testpromise.php";
        ajax(URL).then(function onFulfilled(value) {
            document.write('内容是：' + value);
        }).catch(function onRejected(error) {
            document.write('错误：' + error);
        });
    </script>
    <!-- resolve 方法和 reject 方法调用时，都带有参数。它们的参数会被传递给回调函数。reject 方法的参数通常是 Error 对象的实例， -->
</body>

</html>